import { Spriteset } from "@/containers/Spriteset.js";
import { ImageManager } from "@/managers/ImageManager.js";

import { Container } from "pixi.js";
import { config } from "@/managers/ConfigManager";
import { Sprite_Tiling } from "@/sprites/Sprite_Tiling.js";

import { Sprite_Enemy } from "@/sprites/Sprite_Enemy.js";
import { Sprite_Actor } from "@/sprites/Sprite_Actor.js";

export class Spriteset_Battle extends Spriteset {
    constructor() {
        super();
        
        this.createBattleback();
        this.createBattleField();
        this.createEnemies();
        this.createActors();
    }

    destroy(opts){
        super.destroy(opts);
        this._enemySprites.length=0;
        this._actorSprites.length=0;
    }

    createBattleback() {
        this._back1Sprite = new Sprite_Tiling(null, config.width, config.height);
        this._back2Sprite = new Sprite_Tiling(null, config.width, config.height);
        ImageManager.loadBattleback1(this.battleback1Name()).then(source => {
            this._back1Sprite.source = source;
        });

        ImageManager.loadBattleback2(this.battleback2Name()).then(source => {
            this._back2Sprite.source = source;
        });
        this.base.addChild(this._back1Sprite);
        this.base.addChild(this._back2Sprite);
    }

    createBattleField() {
        this._battleField = new Container();
        this.base.addChild(this._battleField);
    }

    createEnemies() {
        this._enemySprites=[];
        let enemies = $gameTroop.members();
        for(let enemy of enemies){
            let sprite=new Sprite_Enemy();
            sprite.setBattler(enemy);
            this._enemySprites.push(sprite);
            this._battleField.addChild(sprite);
        }
    }

    createActors() {
        this._actorSprites = [];
        let members=$gameParty.members();
        for(let member of members){
            let sprite=new Sprite_Actor();
            sprite.setBattler(member);
            this._actorSprites.push(sprite);
            this._battleField.addChild(sprite);
        }
    }

    isBusy(){
        return this._enemySprites.some(sprite=>sprite.isBusy())||
        this._actorSprites.some(sprite=>sprite.isBusy());
    }

    update(){
        super.update();
        for(let child of this._battleField.children){
            child?.update();
        }
    }

    battleback1Name() {
        // if (BattleManager.isBattleTest()) {
        //     return $dataSystem.battleback1Name;
        // } else 
        if ($gameMap.battleback1Name()) {
            return $gameMap.battleback1Name();
        } else if ($gameMap.isOverworld()) {
            return this.overworldBattleback1Name();
        } else {
            return $dataSystem.battleback1Name;
        }
    }

    battleback2Name() {
        // if (BattleManager.isBattleTest()) {
        //     return $dataSystem.battleback2Name;
        // } else 
        if ($gameMap.battleback2Name()) {
            return $gameMap.battleback2Name();
        } else if ($gameMap.isOverworld()) {
            return this.overworldBattleback2Name();
        } else {
            return $dataSystem.battleback2Name;
        }
    }

    overworldBattleback1Name() {
        if (!$gameMap.battleback1Name()) return '';
        if ($gamePlayer.isInVehicle()) {
            return this.shipBattleback1Name();
        } else {
            return this.normalBattleback1Name();
        }
    }

    overworldBattleback2Name() {
        if (!$gameMap.battleback2Name()) return '';
        if ($gamePlayer.isInVehicle()) {
            return this.shipBattleback2Name();
        } else {
            return this.normalBattleback2Name();
        }
    }

    normalBattleback1Name() {
        return (this.terrainBattleback1Name(this.autotileType(1)) ||
            this.terrainBattleback1Name(this.autotileType(0)) ||
            this.defaultBattleback1Name());
    }

    normalBattleback2Name() {
        return (this.terrainBattleback2Name(this.autotileType(1)) ||
            this.terrainBattleback2Name(this.autotileType(0)) ||
            this.defaultBattleback2Name());
    }

    terrainBattleback1Name(type) {
        switch (type) {
            case 24: case 25:
                return 'Wasteland';
            case 26: case 27:
                return 'DirtField';
            case 32: case 33:
                return 'Desert';
            case 34:
                return 'Lava1';
            case 35:
                return 'Lava2';
            case 40: case 41:
                return 'Snowfield';
            case 42:
                return 'Clouds';
            case 4: case 5:
                return 'PoisonSwamp';
            default:
                return null;
        }
    }

    terrainBattleback2Name(type) {
        switch (type) {
            case 20: case 21:
                return 'Forest';
            case 22: case 30: case 38:
                return 'Cliff';
            case 24: case 25: case 26: case 27:
                return 'Wasteland';
            case 32: case 33:
                return 'Desert';
            case 34: case 35:
                return 'Lava';
            case 40: case 41:
                return 'Snowfield';
            case 42:
                return 'Clouds';
            case 4: case 5:
                return 'PoisonSwamp';
            default:
                return null;
        }
    }

    defaultBattlebackName() {return 'Grassland';}
    defaultBattleback2Name() {return 'Grassland';}
    shipBattleback1Name() {return 'Ship';}
    shipBattleback2Name() {return 'Ship';}
    autotileType(z) {return $gameMap.autotileType($gamePlayer.x, $gamePlayer.y, z);}

}